#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS=hardening=+all
include /usr/share/dpkg/architecture.mk

BUILDDIR = obj-$(DEB_HOST_MULTIARCH)

# SSE options
ifeq ($(DEB_HOST_ARCH_CPU),amd64)
CMAKE_ARCH_FLAGS = -DENABLE_SSE=ON -DENABLE_SSE2=ON -DENABLE_SSE3=OFF
else
CMAKE_ARCH_FLAGS = -DENABLE_SSE=OFF -DENABLE_SSE2=OFF -DENABLE_SSE3=OFF
endif

# TBB support
ifneq (,$(findstring $(DEB_HOST_ARCH), amd64 arm64 armel armhf i386 mips mips64el mipsel ppc64el s390x powerpc powerpcspe ppc64 sh4 sparc64))
CMAKE_ARCH_FLAGS += -DWITH_TBB=ON
else
CMAKE_ARCH_FLAGS += -DWITH_TBB=OFF
endif

# Linux-specific stuff
ifeq ($(DEB_HOST_ARCH_OS),linux)
CMAKE_ARCH_FLAGS += -DWITH_1394=ON -DWITH_V4L=ON
else
CMAKE_ARCH_FLAGS += -DWITH_1394=OFF -DWITH_V4L=OFF
endif

# armhf options
ifeq ($(DEB_HOST_ARCH),armhf)
CMAKE_ARCH_FLAGS = -DENABLE_NEON=ON -DENABLE_VFPV3=ON
endif

# For Java
export JAVA_HOME=/usr/lib/jvm/default-java

CMAKE_FLAGS = \
	-DCMAKE_INSTALL_PREFIX=/usr \
	-DCMAKE_INSTALL_LIBDIR="lib/$(DEB_HOST_MULTIARCH)" \
	-DCMAKE_VERBOSE_MAKEFILE=ON \
	-DCMAKE_C_FLAGS_RELEASE="$(CFLAGS)" \
	-DCMAKE_CXX_FLAGS_RELEASE="$(CXXFLAGS)" \
	-DCMAKE_EXE_LINKER_FLAGS_RELEASE="$(LDFLAGS)" \
	-DOPENCV_MATHJAX_RELPATH=/usr/share/javascript/mathjax/ \
	-DCMAKE_BUILD_TYPE=Release	\
	-DBUILD_EXAMPLES=ON	\
	-DINSTALL_C_EXAMPLES=ON	\
	-DINSTALL_PYTHON_EXAMPLES=ON	\
	-DWITH_FFMPEG=ON	\
	-DWITH_GSTREAMER=OFF	\
	-DWITH_GTK=ON	\
	-DWITH_JASPER=OFF	\
	-DWITH_JPEG=ON	\
	-DWITH_PNG=ON	\
	-DWITH_TIFF=ON	\
	-DWITH_OPENEXR=ON \
	-DWITH_PVAPI=ON	\
	-DWITH_UNICAP=OFF	\
	-DWITH_EIGEN=ON \
	-DWITH_VTK=ON \
	-DWITH_GDAL=ON \
	-DWITH_GDCM=ON \
	-DWITH_XINE=OFF	\
	-DWITH_IPP=OFF \
	-DBUILD_TESTS=OFF \
	-DCMAKE_SKIP_RPATH=ON \
	-DWITH_CUDA=OFF \
	-DENABLE_PRECOMPILED_HEADERS=OFF \
	-DWITH_IPP=OFF \
	-DWITH_CAROTENE=OFF \
	-DOPENCL_INCLUDE_DIR:PATH="/usr/include/CL/" \
	-DOPENCV_EXTRA_MODULES_PATH="$(CURDIR)/contrib/modules" \
	$(CMAKE_ARCH_FLAGS)

%:
	dh $@ --with python2,python3,javahelper,jh_maven_repo_helper --parallel

override_dh_clean:
	rm -rvf modules/python/src2/hdr_parser.pyc
	rm -rvf modules/java/generator/rst_parser.pyc
	rm -rvf modules/refman.rst

	dh_clean
	-rm -rvf modules/python/src2/__pycache__

override_dh_auto_clean:
	dh_auto_clean -B $(BUILDDIR)
	dh_auto_clean -B $(BUILDDIR)-static


override_dh_auto_configure: configure_dyn configure_static
configure_dyn:
	dh_auto_configure -B $(BUILDDIR) \
		-- $(CMAKE_FLAGS) \
		-DCMAKE_SHARED_LINKER_FLAGS_RELEASE="$(LDFLAGS)" \
		-DBUILD_SHARED_LIBS=ON -DBUILD_DOCS=ON
configure_static:
	dh_auto_configure -B $(BUILDDIR)-static \
		-- $(CMAKE_FLAGS) \
		-DBUILD_SHARED_LIBS=OFF -DBUILD_DOCS=OFF


override_dh_auto_build: build_dyn build_static
build_dyn:
	dh_auto_build -B $(BUILDDIR)
	dh_auto_build -B $(BUILDDIR) -- doxygen
build_static:
	dh_auto_build -B $(BUILDDIR)-static

override_dh_auto_test:

override_dh_auto_install:
	dh_auto_install -B $(BUILDDIR)

override_dh_install:
	# change install path of java library
	sed -i -e "s@share\/OpenCV\/java\/@lib\/jni\/@g" \
		debian/tmp/usr/share/OpenCV/OpenCVModules-release.cmake

	# put the static libs together with the rest of the stuff
	cp -v $(BUILDDIR)-static/lib/*.a debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/

	rm -vrf debian/tmp/usr/share/OpenCV/doc/html/jquery.js

	find debian/tmp -type d -empty -delete
	dh_install --fail-missing

override_dh_gencontrol:
	dh_numpy
	dh_numpy3
	dh_gencontrol

OCV_CONTRIB = opencv_contrib-3.2.0
PKG = opencv
UVER = $(shell dpkg-parsechangelog -S Version | sed 's,-.*,,')
DTYPE = +dfsg
VER  ?= $(subst $(DTYPE),,$(UVER))
get-orig-source:
	@echo "# OpenCV Downloading..."
	uscan --no-conf --verbose --force-download --download-version $(VER) --no-symlink
	@echo "# Packing..."
	mk-origtargz --repack --compression xz -S $(DTYPE) -C .. ../opencv_$(VER).orig.tar.gz -v $(VER)

	@echo "# OpenCV contrib Downloading..."
	uscan --no-conf --verbose --force-download --download-version $(VER) --no-symlink
	mk-origtargz --repack --compression xz -S $(DTYPE) -C .. ../opencv_$(VER).orig-contrib.tar.gz -v $(VER) -c contrib
